#include "my_inc/main_login.h"
#include "my_inc/tic.h"

static int row    = 0;
static int column = 0;
static int count  = 0;

/**
 * @brief 刷新游戏UI
 */
void Chessboard(lv_obj_t * cont_tic, char sign[SIZE][SIZE])
{
    if(!cont_tic) return;

    // 删除现有的所有子对象（方块）
    lv_obj_clean(cont_tic);

    int count = 0;
    for(size_t i = 0; i < 3; i++) {
        for(size_t j = 0; j < 3; j++) {
            lv_obj_t * obj = lv_obj_create(cont_tic);
            lv_obj_set_style_border_width(obj, 2, LV_PART_MAIN); // 边框宽度
            lv_obj_set_style_border_color(obj, lv_color_hex(0x776e65), LV_PART_MAIN);
            lv_obj_set_style_radius(obj, 4, LV_PART_MAIN);
            lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, j, 1, LV_GRID_ALIGN_STRETCH, i, 1);

            lv_obj_t * labell = lv_label_create(obj);
            char num_str[10];
            snprintf(num_str, sizeof(num_str), "%d", ++count);
            lv_label_set_text(labell, num_str);
            lv_obj_add_flag(labell, LV_OBJ_FLAG_HIDDEN); // 隐藏OBJ
            lv_obj_add_event_cb(obj, tic_cell, LV_EVENT_CLICKED, labell);

            lv_obj_t * label = lv_label_create(obj);
            char text[2]     = {sign[i][j], '\0'}; // 将字符转换为字符串
            lv_label_set_text(label, text);
            lv_obj_set_style_text_font(label, &lv_font_montserrat_48, LV_PART_MAIN); // 设置字体大小
            lv_obj_center(label);
        }
    }
}

// 棋盘初始化
void init_tic()
{
    for(size_t i = 0; i < SIZE; i++) {
        for(size_t j = 0; j < SIZE; j++) {
            sign[i][j] = ' ';
        }
    }
    count  = 0;
}

// 释放内存
void cleanup()
{
    for(size_t i = 0; i < SIZE; i++) {
        free(sign[i]);
    }
}

// 玩家写入
bool player(char a, main_tic_p tic_p)
{
    lv_obj_t * result_label = re_label();
    // 获取按钮文本内容，用于判断是哪个按钮触发的事件
    if(result_label == NULL) {
        return 0;
    }

    const char * label_text = lv_label_get_text(result_label);
    char temp               = (char)*label_text - 49;
    if (temp / 3 == row && temp % 3 == column)
    {
        lv_timer_pause(timer); // 在这里暂停计时器
        return 0;
    }
    row                     = temp / 3;
    column                  = temp % 3;
    printf("%d %d\n", row, column);
    if(row >= 0 && row < SIZE && column >= 0 && column < SIZE) {
        if(sign[row][column] == ' ') {
            sign[row][column] = a;
            count++;
            Chessboard(tic_p->cont_tic, sign);
            return 1;
        }
    }
}

// 判断是否有人取胜
bool judge(char a, main_tic_p tic_p)
{
    // 检查行
    for(size_t i = 0; i < SIZE; i++) {
        if(sign[i][0] == a && sign[i][1] == a && sign[i][2] == a) {
            char temp_data[20] = " ";
            sprintf(temp_data, "%c玩家获胜!\n", a);
            lv_obj_clear_flag(tic_p->game_over_label, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
            lv_label_set_text(tic_p->game_over_label, temp_data);
            lv_timer_del(game_timer);
            game_timer = NULL; // 将指针设为 NULL，避免野指针
            return 1;
        }
    }

    // 检查列
    for(size_t j = 0; j < SIZE; j++) {
        if(sign[0][j] == a && sign[1][j] == a && sign[2][j] == a) {
            char temp_data[20] = " ";
            sprintf(temp_data, "%c玩家获胜!\n", a);
            lv_obj_clear_flag(tic_p->game_over_label, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
            lv_label_set_text(tic_p->game_over_label, temp_data);
            lv_timer_del(game_timer);
            game_timer = NULL; // 将指针设为 NULL，避免野指针
            return 1;
        }
    }
    // 检查对角线
    if(sign[0][0] == a && sign[1][1] == a && sign[2][2] == a) {
        char temp_data[20] = " ";
        sprintf(temp_data, "%c玩家获胜!\n", a);
        lv_obj_clear_flag(tic_p->game_over_label, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
        lv_label_set_text(tic_p->game_over_label, temp_data);
        lv_timer_del(game_timer);
        game_timer = NULL; // 将指针设为 NULL，避免野指针
        return 1;
    } else if(sign[0][2] == a && sign[1][1] == a && sign[2][0] == a) {
        char temp_data[20] = " ";
        sprintf(temp_data, "%c玩家获胜!\n", a);
        lv_obj_clear_flag(tic_p->game_over_label, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
        lv_label_set_text(tic_p->game_over_label, temp_data);
        lv_timer_del(game_timer);
        game_timer = NULL; // 将指针设为 NULL，避免野指针
        return 1;
    }
    // 检查平局
    else if(count == SIZE * SIZE) {
        lv_obj_clear_flag(tic_p->game_over_label, LV_OBJ_FLAG_HIDDEN); // 显示OBJ
        lv_label_set_text(tic_p->game_over_label, "平局！！！");
        lv_timer_del(game_timer);
        game_timer = NULL; // 将指针设为 NULL，避免野指针
        return 1;
    }
    return 0;
}

// ＃逻辑
void main_tic(lv_timer_t * timer)
{
    main_tic_p tic_p = (main_tic_p)timer->user_data;

    if(tic_p->temp == 0) {
        if(player(tic_p->a[0], tic_p)) {
            judge(tic_p->a[0], tic_p);
            // printf("Player %c wins!\n", tic_p->a[0]);
            lv_timer_pause(timer); // 在这里暂停计时器
            tic_p->temp = 1;
            return;
        }
    } else {
        if(player(tic_p->a[1], tic_p)) {
            judge(tic_p->a[1], tic_p);
            // printf("Player %c wins!\n", tic_p->a[1]);
            lv_timer_pause(timer); // 在这里暂停计时器
            tic_p->temp = 0;
            return;
        }
    }
}